package com.jhws.sys.appUser.api;

import com.jhws.business.comm.bo.TblCommDimBo;
import com.jhws.business.property.service.PropertyService;
import com.jhws.sys.appUser.bo.TblSipAcountBindings;
import com.jhws.business.owner.bo.OwnerConstant;
import com.jhws.business.owner.bo.TblOwnerDimBo;
import com.jhws.business.owner.service.ITblOwnerDimService;
import com.jhws.common.base.bo.JsonRespData;
import com.jhws.business.device.bo.UserDevice;
import com.jhws.sys.appUser.bo.UserInfoV4;
import com.jhws.common.base.bo.Global;
import com.jhws.business.comm.service.ITblCommDimService;
import com.jhws.sys.appUser.service.ITblSipAcountDimService;
import com.jhws.util.AccountsUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(("/v4/appUser"))
public class appUserApiV4 extends appUserApiV3 {

    @Resource
    ITblOwnerDimService IOwnerService;
    @Resource
    ITblCommDimService ICommService;
    @Resource
    ITblSipAcountDimService ISipAccountService;
    @Resource
    PropertyService propertyService;
    @Override
    public JsonRespData getUserInfoList(String username) throws Exception {
        List<UserInfoV4> uList = new ArrayList<>();
        /* 获取所有社区角色 */
        List<TblOwnerDimBo> ownerList = IOwnerService.findTblOwnerDimByWhere("where owner_mob = '"+username+"' and is_enable='0' ");
        String roleType = "";

        for(TblOwnerDimBo owner : ownerList){
            List<UserDevice> devicesList = null;
            Map managerAttach = null;
            TblCommDimBo comm = ICommService.findTblCommDimById(owner.getCommCode());
            int applyType = owner.getApplyType();
            if(applyType == OwnerConstant.OWNER_TYPE_PROPERTY_MANAGER){
                applyType = 0;
                roleType = OwnerConstant.ROLE_MANAGER_TYPE;
                devicesList = ICommService.genDeviceListForRole(OwnerConstant.ROLE_MANAGER_TYPE,owner.getRoomInfoNum());
                managerAttach = propertyService.getStaffInfoByOwnerCode(owner.getOwnerCode());
            }else if(applyType == OwnerConstant.OWNER_TYPE || applyType == OwnerConstant.OWNER_TYPE_RENTER || applyType == OwnerConstant.OWNER_TYPE_VILLA) {
                applyType = OwnerConstant.OWNER_TYPE;
                roleType = OwnerConstant.ROLE_OWNER_TYPE;
                devicesList = ICommService.genDeviceListForRole(OwnerConstant.ROLE_OWNER_TYPE, owner.getRoomInfoNum());
            }else if(applyType == OwnerConstant.OWNER_TYPE_APPLYING || applyType == OwnerConstant.OWNER_TYPE_REJECTED){
                roleType = OwnerConstant.ROLE_OWNER_TYPE;
            }else if(applyType == OwnerConstant.OWNER_TYPE_FAMILY){
                applyType = 0;
                roleType = OwnerConstant.ROLE_FAMILY_TYPE;
                devicesList = ICommService.genDeviceListForRole(OwnerConstant.ROLE_OWNER_TYPE, owner.getRoomInfoNum());
            }

            UserInfoV4 user = new UserInfoV4(applyType,owner.getRoomInfoNum(),owner.getCommCode(),owner.getCommName(),
                    roleType,comm.getCommSoftVersion(),owner.getOwnerCode());
            user.setAreaAliasName(AccountsUtils.getAreaNameFromRoomInfo(owner.getRoomInfo()));
            user.setBuildingAliasName(AccountsUtils.getBuildingNameFromRoomInfo(owner.getRoomInfo()));
            user.setDevicesList(devicesList);
            user.setManagerAttach(managerAttach);
            uList.add(user);
        }

        /* 查看是否有别墅角色 */
        List<TblSipAcountBindings> bindings = ISipAccountService.findTblSipAcountBindingsByWhere("where username = '"+username+"' and binding_type = '"+Global.BINDING_TYPE_VILLA+"'");
        if(bindings!=null && bindings.size()!=0){
            UserInfoV4 user = new UserInfoV4(0,"villa","0000","别墅",OwnerConstant.ROLE_VILLA_TYPE,0,null);
            List<UserDevice> deviceList = new ArrayList<>();
            for(TblSipAcountBindings binding : bindings){
                deviceList.add(new UserDevice(binding.getBinding(),"别墅机-"+binding.getBinding().substring(7,8)));
            }
            user.setDevicesList(deviceList);
            uList.add(user);
        }
        return new JsonRespData("getAllBindingList",1,uList);
    }
}
